﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec.NguoiDung
{
    public partial class Apply : System.Web.UI.Page
    {
        public int TuyenDungId { set; get; }
        public string Url { set; get; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] !="")
            {
                TuyenDungId = Convert.ToInt32(Request.QueryString["id"]);
            }
            else
            {
                Response.Redirect("/");
            }
            if (Request.QueryString["url"] !="")
            {
                Url = Request.QueryString["url"];
            }

            else
            {
                Response.Redirect("/");
            }
            if(!IsPostBack)
            {

                LoadData(TuyenDungId);
            }
        }

        private void LoadData(int tuyenDungId)
        {
            if(Session["NguoiDung"]!=null)
            {
                DAL.User user = BLL.NguoiDung.NguoiDung.Get_UserEmail(Session["NguoiDung"].ToString());

                var tuyenDung = BLL.TuyenDung.DangTuyenHelper.Get_TinTuyenDung(tuyenDungId);
                if(tuyenDung!=null)
                {
                    lbChucDanh.Text = tuyenDung.ChucDanh;
                    lbTenCty.Text = tuyenDung.TenCongTy;
                    lbEmail.Text = user.Email;
                }
                if(user!=null)
                {
                    var lstHoSo = BLL.NguoiDung.HoSo.GetHoSoHoanThanh(user.UserId);

                    droHoSo.DataSource = lstHoSo;
                    droHoSo.DataTextField = "TieuDe";
                    droHoSo.DataValueField = "HoSoId";
                    droHoSo.DataBind();
                }


            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            if (Session["NguoiDung"] != null)
            {
                DAL.User user = BLL.NguoiDung.NguoiDung.Get_UserEmail(Session["NguoiDung"].ToString());

                if(BLL.NguoiDung.HoSo.Apply(user.UserId,Convert.ToInt32(droHoSo.SelectedValue),TuyenDungId))
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe",
                                                            "alert('Nộp đơn thành công!');", true);
                    Response.Redirect("/nguoi-dung/cong-viec-ung-tuyen.html");
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe",
                                                            "alert('Bạn đã nộp hồ sơ cho tin tuyển dụng này!');", true);
                }
            }
        }

        protected void btQuayLai_Click(object sender, EventArgs e)
        {
            Response.Redirect("/tim-viec-lam/"+TuyenDungId+"/"+Url+ ".html");
        }
    }
}